﻿
namespace MyFurion.Application
{
    /// <summary>
    /// 角色信息服务仓储
    /// </summary>
    public class RoleRepository:BaseRepository<RoleInfo>, ITransient
    {
        /// <summary>
        /// 查询条件
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        private Expression<Func<RoleInfo, bool>> GetCodition(RoleInfoPage input)
        {
            var exp = Expressionable.Create<RoleInfo>();
            exp.AndIF(!string.IsNullOrWhiteSpace(input.RoleCode), it => it.RoleCode.Contains(input.RoleCode));
            exp.AndIF(!string.IsNullOrWhiteSpace(input.RoleName), it => it.RoleName.Contains(input.RoleName));
            exp.AndIF(input.Status.HasValue, it => it.Status == input.Status.Value);
            return exp.ToExpression();
        }
        /// <summary>
        /// 获取分页数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PageResult<RoleInfo>>GetPageList(RoleInfoPage input)
        {
            var whereExp = GetCodition(input);
            return await GetPageListAsync(whereExp, input.PageIndex, input.PageSize, it => it.CreateTime);
        }
        /// <summary>
        /// 获取数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<List<RoleInfo>> GetDataList(RoleInfoPage input)
        {
            var whereExp = GetCodition(input);
            return await GetList(whereExp,it => it.CreateTime);
        }
        /// <summary>
        /// 是否空表
        /// </summary>
        /// <returns></returns>
        public async Task<bool> IsEmptyData()
        {
            int count = await Context.Queryable<RoleInfo>().Filter(null, true).CountAsync();
            return count == 0;
        }
        /// <summary>
        /// 数据是否存在
        /// </summary>
        /// <param name="roleCode"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<bool>DataExists(string roleCode, long? id = null)
        {
            return await Context.Queryable<RoleInfo>()
                .Where(it => it.RoleCode == roleCode)
                .WhereIF(id.HasValue, it => it.Id != id.Value)
                .AnyAsync();
        }
        /// <summary>
        /// 获取用户最高机构数据权限信息
        /// </summary>
        /// <param name="userId">用户id</param>
        /// <param name="userOrgId">用户所属机构id</param>
        /// <returns></returns>
        public async Task<OrgPerDto> GetOrgPer(long userId,long userOrgId)
        {
            var roleInfo= await Context.Queryable<RoleInfo>().InnerJoin<UserRoleInfo>((r, l) => r.Id == l.RoleId)
                .Where((r, l) => l.UserId == userId).OrderByDescending((r,l)=>r.OrgAuthority)
                .Select((r,l)=>new { RoleId=r.Id,r.OrgAuthority}).FirstAsync();
            if (roleInfo != null)
            {
                List<long> orgIds = new();
                var orgAuth= roleInfo.OrgAuthority;
                if (orgAuth == EnumOrgAuthority.CustomOrg)
                {
                    var orgPerInfo = await Context.Queryable<OrgPerInfo>().Where(it => it.RoleId == roleInfo.RoleId).ToListAsync();
                    if (orgPerInfo.Any())
                    {
                        orgIds = orgPerInfo.Select(g => g.OrgId).Distinct().ToList();
                    }
                }
                else if (orgAuth == EnumOrgAuthority.OrgChild)
                {
                    var childrenOrgIds = await Context.Queryable<OrgInfo>().ToChildListAsync(it=>it.ParentId,userOrgId);
                    orgIds = childrenOrgIds.Select(g => g.Id).ToList();
                }
                OrgPerDto orgPer = new();
                orgPer.OrgAuthority= orgAuth;
                orgPer.OrgIds = orgIds;
                return orgPer;
            }
            else
            {
                return null;
            }
        }
    }
}
